<template>
  <ContentWrap>
    <div class="mb-10px">
      <ElButton :icon="Plus" type="primary" @click="action({}, 0)">{{
        t('exampleDemo.add')
      }}</ElButton>
      <ElButton :icon="Delete" :loading="delLoading" @click="delData(null, true)">
        {{ t('menuTable.AllDel') }}</ElButton
      >
    </div>

    <Table
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      row-key="id"
      @register="register"
    >
      <template #action="{ row }">
        <span class="el-btn" @click="action(row, 1)"> {{ t('exampleDemo.edit') }}</span>
        <span
          class="el-btn"
          :ref="(el) => handleRef(el, row)"
          @click="handleBtn(refMap[row.id], row)"
        >
          {{ t('menuTable.more') }}<ElIcon class="el-icon--right"><arrow-down /></ElIcon>
        </span>
        <!-- <ElDropdown style="margin-left: 20px; vertical-align: middle" trigger="click">
          <span class="el-btn">
            {{ t('menuTable.more') }}<ElIcon class="el-icon--right"><arrow-down /></ElIcon>
          </span>
          <template #dropdown>
            <ElDropdownMenu>
              <ElDropdownItem @click="action(row, 2)">{{ t('menuTable.detail') }}</ElDropdownItem>
              <ElDropdownItem @click="action(row, 3)">{{ t('menuTable.add') }}</ElDropdownItem>
              <ElDropdownItem @click="delData(row, false)">{{ t('menuTable.del') }}</ElDropdownItem>
            </ElDropdownMenu>
          </template>
        </ElDropdown> -->
      </template>
    </Table>
  </ContentWrap>
  <ElPopover v-model:visible="visiblePopover" :virtual-ref="tempRef" popper-class="TablePop">
    <p @click="action(objItem, 2)">{{ t('menuTable.detail') }}</p>
    <p @click="action(objItem, 3)">{{ t('menuTable.add') }}</p>
    <p @click="delData(objItem, false)">{{ t('menuTable.del') }}</p>
  </ElPopover>
  <!-- <center v-show="showCenter" :x="mouse_x" :y="mouse_y" @mouseleave="leaveCenter" /> -->
</template>

<script setup lang="ts" name="MenuManageManage">
import { ContentWrap } from '@/components/ContentWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElPopover, ElIcon } from 'element-plus'
import { Table } from '@/components/Table'
import { getMenuTableListApi, delIdApi, deleteBatchIdApi } from '@/api/modules/menu'
import { useTable } from '@/hooks/web/useTable'
import { MenuTableData } from '@/api/table/types'
import { h, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'
// import { debounce } from 'lodash-es'
// import center from '../components/center.vue'
import { ArrowDown, Plus, Delete } from '@element-plus/icons-vue'

const { push } = useRouter()

const { register, tableObject, methods } = useTable<MenuTableData>({
  getListApi: getMenuTableListApi,
  delListApi: delIdApi,
  delAllListApi: deleteBatchIdApi,
  response: {
    list: 'result',
    total: 'total'
  }
})

const { getList } = methods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'title',
    label: t('menuTable.name'),
    width: '260px'
  },
  {
    field: 'menuType',
    label: t('menuTable.menuType'),
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      return h(
        'span',
        { class: 'tag' },
        cellValue === 0 ? '菜单' : cellValue === 1 ? '菜单' : '按钮/权限'
      )
    }
  },
  {
    field: 'icon',
    label: t('menuTable.icon'),
    width: '200px'
  },
  {
    field: 'component',
    label: t('menuTable.component'),
    width: '220px'
  },
  {
    field: 'url',
    label: t('menuTable.path'),
    width: '260px'
  },
  {
    field: 'sortNo',
    label: t('menuTable.sort'),
    width: '100px'
  },
  {
    field: 'action',
    width: '150px',
    label: t('tableDemo.action'),
    fixed: 'right'
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const delLoading = ref(false)

// 删除
const delData = async (row: MenuTableData | null, multiple: boolean) => {
  tableObject.currentRow = row
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple
      ? { ids: selections.map((v) => v.id).join(',') }
      : { id: tableObject.currentRow?.id as string },
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}
// 操作表格
const action = (row: MenuTableData, type: number) => {
  let data = ''
  if (type === 3) {
    // 新增子菜单
    data = `?type=${type}&row=${encodeURIComponent(
      JSON.stringify({ parentId: row.id, menuType: 1, sortNo: 1 })
    )}`
  } else if (type === 0) {
    // 新增
    data = `?type=${type}`
  } else {
    // 编辑 详情
    data = `?type=${type}&row=${encodeURIComponent(JSON.stringify(row))}`
  }
  push(`/system/menuManage/detail${data}`)
}
const objItem = ref({}) // table 每列数据
const refMap = ref({}) // 每个btn绑定ref
const tempRef = ref() // 弹出框绑定btn的ref

const visiblePopover = ref(false)
const handleBtn = (ref, row) => {
  tempRef.value = ref
  objItem.value = row
  visiblePopover.value = true
}
const handleRef = (ref, item) => {
  refMap.value[item.id] = ref
}
</script>
<style scoped>
@import './index.less';
</style>
